package com.unsk.item.player
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2World;
	
	import com.alex.game.framework.BasicItem;
	import com.alex.game.framework.Input;
	import com.unsk.util.Log;
	
	import flash.display.MovieClip;
	import com.unsk.item.Box2DBodyContactListener;
	
	public class PlayerInputDecorator extends BasicItem
	{
		private var player:Player;
		public var isJumping:Boolean = false;
		private var world:b2World;
		
		public function PlayerInputDecorator(player:Player, world:b2World)
		{
			this.world = world;
			this.player = player;
		}
		override public function create():void
		{
			player.create();
			var contactListener:Box2DBodyContactListener =new Box2DBodyContactListener(player.body,isInContact);
			world.SetContactListener(contactListener);
		}
		override public function destroy():void
		{
			player.destroy();
		}
		override public function update():void
		{
			player.update();
		
			var linearVelocity:int = 0;
			
			if(Input.KEY_RIGHT)
				linearVelocity += 6;
			
			if(Input.KEY_LEFT)
				linearVelocity += -6;
			
			player.body.m_linearVelocity.x = linearVelocity;
			
			if(Input.KEY_UP && !isJumping)
			{
				isJumping = true;
				player.body.ApplyImpulse(new b2Vec2(0.0,-6),player.body.GetWorldCenter());
			}
		}
		public function isInContact():void
		{
			if(isJumping)
			{
				isJumping = false;
				Log.debug(this, "Ne saute plus");	
			}
		}
	}
}